package com.shiwaixiangcun.customer.module.heath

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v4.view.ViewPager
import android.util.Log
import android.view.View
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.ui.fragment.LazyFragment
import com.shiwaixiangcun.customer.widget.tablayout.CustomTabEntity
import com.shiwaixiangcun.customer.widget.tablayout.OnTabSelectListener
import kotlinx.android.synthetic.main.layout_fragment_health.*

/**
 * Author   Administrator
 * Date     2018/1/22
 * Desc     健康管理数据
 */

class HealthFragment : LazyFragment() {
    companion object {
        val BUG_TAG = "HealthFragment"
        val instance: HealthFragment
            get() = HealthFragment()

        fun newInstance(userID: Long?): HealthFragment {
            val f = HealthFragment()
            val args = Bundle()
            args.putLong("userID", userID!!)
            f.arguments = args
            return f

        }
    }

    private var userID: Long? = null
    private var mFragments: ArrayList<Fragment>? = null
    private val mTitles = arrayOf("数据", "动态", "体检", "方案")
    private val mIconUnSelectIds = intArrayOf(R.drawable.icon_health_data, R.drawable.icon_health_moment,
            R.drawable.icon_health_report, R.drawable.icon_health_plan)
    private val mIconSelectIds = intArrayOf(R.drawable.icon_health_data, R.drawable.icon_health_moment,
            R.drawable.icon_health_report, R.drawable.icon_health_plan)
    private val mTabEntities = ArrayList<CustomTabEntity>()

    override fun getContentViewLayoutID(): Int {
        return R.layout.layout_fragment_health
    }


    override fun initViewsAndEvents(view: View) {

        val fragmentData = FragmentData.newInstance(userID)
        val fragmentMoment = FragmentMoment.newInstance(userID)
        val fragmentReport = FragmentReport.newInstance(userID)
        val fragmentPlan = FragmentPlan.newInstance(userID)
        mFragments = ArrayList()
        mFragments?.add(fragmentData)
        mFragments?.add(fragmentMoment)
        mFragments?.add(fragmentReport)
        mFragments?.add(fragmentPlan)

        mTitles.indices.mapTo(mTabEntities) { TabEntity(mTitles[it], mIconSelectIds[it], mIconUnSelectIds[it]) }
        viewPager.adapter = MyPagerAdapter(activity?.supportFragmentManager!!)
        tabLayout.setTabData(mTabEntities)
        tabLayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                viewPager.currentItem = position
            }

            override fun onTabReselect(position: Int) {
                if (position == 0) {
//                    mTabLayout_2.showMsg(0, mRandom.nextInt(100) + 1)
                    //                    UnreadMsgUtils.show(mTabLayout_2.getMsgView(0), mRandom.nextInt(100) + 1);
                }
            }
        })

        viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                tabLayout.currentTab = position
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })

        viewPager.currentItem = 0
        viewPager.offscreenPageLimit = 4

    }

    override fun onFirstUserVisible() {

    }

    override fun onUserVisible() {


    }

    override fun onUserInvisible() {

    }

    override fun destroyViewAndThing() {


    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val bundle = arguments
        userID = bundle?.getLong("userID")
        Log.e(BUG_TAG, "userID:${userID}")

    }

    private inner class MyPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm) {

        override fun getCount(): Int {
            return mFragments!!.size
        }

        override fun getPageTitle(position: Int): CharSequence? {
            return mTitles[position]
        }

        override fun getItem(position: Int): Fragment? {
            return mFragments?.get(position)
        }
    }
}




